<?php

class LabController extends Controller
{
	public $layout="//layouts/nolayout";
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		//base
	}

	public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array(
				),
                'users'=>array('@'),
            ),
        );
    }
	
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	
	public function actionIndex()
	{
		$this->pageTitle ="实验室管理系统";
		if (empty(Yii::app()->user->id))
		{
			$this->redirect(array('site/index'));
			return;
		}
		$this->render('index');
	}
	
	public function actionIndexPage()
	{
		$this->render('indexPage');
	}

	public function actionIsLogined()
	{
		if (empty(Yii::app()->user->id))
		{
			$json=array(  
						'success'=>false,
						'res'=>'用户未登录'
					);   
			echo CJSON::encode($json);
		}else{
			$json=array(  
						'success'=>true,
						'res'=>''
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionGetModuleList()
	{
		$json=Module::model()->findAll();   
		echo CJSON::encode($json);
	}
	
	public function actionEnableModule($name)
	{
		$user=User::model()->findByPk(Yii::app()->user->id);
		$role=$user->Role;
		if (empty($role))
		{
			$json=array(  
						'success'=>false,
					);   
			echo CJSON::encode($json);
			return;
		}
		
		$module=Module::model()->findByAttributes(array('name'=>$name));
		if (empty($module))
		{
			$json=array(  
						'success'=>false,
					);   
			echo CJSON::encode($json);
			return;
		}
		
		$enableFlag=Role_Module::model()->findByAttributes(array('role_id'=>$role->role_id,'module_id'=>$module->module_id));
		if (!empty($enableFlag))
		{
			$json=array(  
						'success'=>true,
					);   
			echo CJSON::encode($json);
		}else{
			$json=array(  
						'success'=>false,
					);   
			echo CJSON::encode($json);
		}
	}
}